package com.migu.contacts.main.my;

import static com.migu.contacts.net.Service.API;
import static com.migu.contacts.util.DialogUtil.showVIPDialog;

import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.BottomPopup;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.LabelBean;
import com.migu.contacts.databinding.ActivityUserLabelBinding;
import com.migu.contacts.databinding.ItemUserLabel2Binding;
import com.migu.contacts.databinding.ItemUserLabelBinding;
import com.migu.contacts.databinding.LabelAddBinding;
import com.migu.contacts.databinding.PopLabelDeleteBinding;
import com.migu.contacts.net.ResultCallback;
import com.migu.contacts.util.UserCenter;

import java.util.ArrayList;
import java.util.List;

public class UserLabelActivity extends BaseActivity<ActivityUserLabelBinding> {

    @BindAdapter(R.id.topRecyclerView)
    BaseAdapter<ItemUserLabelBinding, LabelBean> topAdapter = new BaseAdapter<ItemUserLabelBinding, LabelBean>() {
    };
    @BindAdapter(R.id.bottomRecyclerView)
    BaseAdapter<ItemUserLabel2Binding, LabelBean> bottomAdapter = new BaseAdapter<ItemUserLabel2Binding, LabelBean>() {
        @OnClick(R.id.close)
        public void click(LabelBean bean, int position) {
            showDeleteDialog(bean, position);
        }
    };

    private void showDeleteDialog(LabelBean bean, int position) {
        CenterPopup bottomPopup = new CenterPopup<PopLabelDeleteBinding>(mActivity) {
            @OnClick(R.id.confirm)
            public void confirm() {
                Log.d(TAG, "confirm() called");
                API.labelDelete(bean.id).enqueue(new ResultCallback<Boolean>() {
                    @Override
                    protected void onSuccess(Boolean aBoolean) {
                        if (aBoolean != null && aBoolean) {
                            bottomAdapter.getData().remove(position);
                            bottomAdapter.notifyDataSetChanged();
                        }
                    }
                });
                dismiss();
            }

            @OnClick(R.id.quit)
            public void quit() {
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }

    @Override
    protected void initView() {
        super.initView();
        FlexboxLayoutManager layoutManager = (FlexboxLayoutManager) mBinding.topRecyclerView.getLayoutManager();
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager = (FlexboxLayoutManager) mBinding.bottomRecyclerView.getLayoutManager();
        layoutManager.setFlexDirection(FlexDirection.ROW);
    }

    @Override
    protected void initData() {
        super.initData();
        API.queryLabel().watch(this, new HttpCallback<List<LabelBean>>() {
            @Override
            protected void onSuccess(List<LabelBean> labelBeans) {
                Log.d(TAG, "onSuccess() called with: labelBeans = [" + labelBeans + "]");
                if (labelBeans == null) {
                    return;
                }
                List<LabelBean> topList = new ArrayList<>();
                List<LabelBean> bottomList = new ArrayList<>();
                for (LabelBean labelBean : labelBeans) {
                    if (labelBean.type == 0) {
                        topList.add(labelBean);
                    } else {
                        bottomList.add(labelBean);
                    }
                }
                topAdapter.setData(topList);
                bottomAdapter.addData(bottomList);
            }
        });
    }

    public void create(View view) {
        if (bottomAdapter.getData().size() >= 5 && !UserCenter.isVip()) {
            showVIPDialog(mActivity);
            return;
        }
        BottomPopup bottomPopup = new BottomPopup<LabelAddBinding>(mActivity) {
            @OnClick(R.id.close)
            public void quit() {
                dismiss();
            }

            @Override
            protected void initData() {
                super.initData();
                mBinding.setBean(new LabelBean());
            }

            @OnClick(R.id.confirm)
            public void confirm() {
                API.addLabel(mBinding.getBean()).watch(mActivity, new HttpCallback<LabelBean>() {
                    @Override
                    protected void onSuccess(LabelBean bean) {
                        bottomAdapter.getData().add(bean);
                        bottomAdapter.notifyDataSetChanged();
                        Log.d(TAG, "onSuccess() called with: bean = [" + bean + "]");
                        dismiss();
                    }
                });
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }
}